package com.zzw.thread;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CountDownLatchTest implements Runnable{

    private static int num = 0;
    private static final int thread_nums = 100;
    private static CountDownLatch countDownLatch = new CountDownLatch(10);
    private Logger logger = Logger.getLogger(getClass());

    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        for(int i = 0;i < thread_nums;i++){
            new Thread(new CountDownLatchTest()).start();
            countDownLatch.countDown();
        }
    }

    public void run() {

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.lock();
        num++;
        logger.info(Thread.currentThread().getName() + "-------num:" + num);
        lock.unlock();
    }
}
